import AudioManager from "../AudioManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BButton extends cc.Component {

    @property(cc.Integer)
    index: number = 0;

    @property(cc.Float)
    scaleRatio: number = 0.95;

    isLocked = false;

    private defaultScaleX: number;
    private defaultScaleY: number;
    private touchDownEvent: Function = null;
    private touchUpEvent: Function = null;
    private touchDownEventThisObj: any = null;
    private touchUpEventThisObj: any = null;
    private sleepSeconds: boolean;
    private isSleeping: boolean;

    onLoad() {
        this.defaultScaleX = this.node.scaleX;
        this.defaultScaleY = this.node.scaleY;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchDown, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchUp, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    protected onTouchDown(e: cc.Event.EventTouch) {
        if (this.enabled && !this.isLocked) {
            AudioManager.playFX('common/fx_Click');
            if (this.sleepSeconds) {
                if (this.isSleeping) {
                    return;
                }
            }
            this.node.scaleX = this.defaultScaleX * this.scaleRatio;
            this.node.scaleY = this.defaultScaleY * this.scaleRatio;
            if (this.touchDownEvent) {
                this.touchDownEvent.call(this.touchDownEventThisObj, e, this.index);
            }
        }
    }

    protected onTouchUp(e: cc.Event.EventTouch) {
        this.restore();
        if (this.enabled && !this.isLocked) {
            if (this.sleepSeconds) {
                if (this.isSleeping) {
                    return;
                }
            }
            if (this.touchUpEvent) {
                this.touchUpEvent.call(this.touchUpEventThisObj, e, this.index);
            }
            if (this.sleepSeconds) {
                this.isSleeping = true;
                this.scheduleOnce(() => {
                    this.isSleeping = false;
                }, 1);
            }
        }
    }

    protected onTouchCancel() {
        this.restore();
    }

    restore() {
        this.node.scaleX = this.defaultScaleX;
        this.node.scaleY = this.defaultScaleY;
    }

    setTouchDownEvent(func: Function, thisObj?: any) {
        this.touchDownEvent = func;
        this.touchDownEventThisObj = thisObj;
    }

    setTouchUpEvent(func: Function, thisObj?: any) {
        this.touchUpEvent = func;
        this.touchUpEventThisObj = thisObj;
    }

    /** 显示或隐藏 */
    static show(target: cc.Node, b: boolean) {
        if (!target) {
            console.error('target为null');
            return;
        }
        let btn = target.getComponent(BButton);
        if (!btn) {
            btn = target.addComponent(BButton);
        }
        btn.enabled = b;
        cc.tween(target).to(0.5, { opacity: b ? 255 : 0 }).start();
    }

    /**
     * 给Node添加按钮组件
     * @param target 目标Node
     * @param index 按钮索引，点击时在第二个参数传送
     * @param scaleRatio 按下时缩放比例，默认0.95
     * @param sleepSeconds 是否让按钮休眠1秒，以防止频繁点击，默认true
     */
    static addButton(target: cc.Node, index: number = 0, scaleRatio: number = 0.95, sleepSeconds: boolean = true): BButton {
        if (!target) {
            console.error('target为null');
            return;
        }
        let btn = target.getComponent(BButton);
        if (!btn) {
            btn = target.addComponent(BButton);
        }
        btn.enabled = true;
        btn.index = index;
        btn.scaleRatio = scaleRatio;
        btn.sleepSeconds = sleepSeconds;
        return btn;
    }

    static setEnable(target: cc.Node, b: boolean) {
        if (!target) {
            console.error('target为null');
            return;
        }
        let btn = target.getComponent(BButton);
        if (btn) {
            btn.enabled = b;
        }
    }

    static setLock(target: cc.Node, b: boolean) {
        if (!target) {
            console.error('target为null');
            return;
        }
        let btn = target.getComponent(BButton);
        if (btn) {
            btn.isLocked = b;
        }
    }
	
	static isEnable(target: cc.Node): boolean {
        if (!target) {
            console.error('target为null');
            return false;
        }
        let btn = target.getComponent(BButton);
        if (btn) {
            return btn.enabled;
        }
        return false;
    }

}
